﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!--自适应界面,如果出现,在某些设备出现界面偏小的话,检查一下有没有加入这句 -->
    <meta http-equiv="Content-type" name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">

    <link href="../../../js/jqmobile/jquery.mobile-1.4.5.min.css" rel="stylesheet" />
    <script src="../../../js/jquery.min2.1.4.js"></script>
    <script src="../../../js/jqmobile/jquery.mobile-1.4.5.min.js"></script>
    <script src="../../../js/cordova-1.5.0.js"></script>
    <script src="../../../js/jshelp.js"></script>
    <script src="../../../js/layer/layer.js"></script>
    <script>
        var clickCount = 1;
        //接受传参
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);  //匹配目标参数
            if (r != null) return unescape(decodeURI(r[2])); return null; //返回参数值
        }
        var ids = getUrlParam('id');
        var zt = getUrlParam('zt');
        var customerCode = getUrlParam('customerCode');
        var mobile = getUrlParam('mobile');
        var docno;
        var addclickCount = 0;
        var arrtm = [];
 

        $(function () {
            if (zt == "1") {
                $("#saveandtj").hide();
            } else if (zt == "0") {
                $("#config").hide();
            }
            $.get("ashx/list.ashx?type=getinfo_byids&ids=" + ids + "&a=" + Math.random(),
                { Action: "get" },
                function (data, textStatus) {
                    if (data.itemMain.length > 0) {
                        $("#txt_storename").val(data.itemMain[0].storename);
                        $("#txt_shr").val(data.itemMain[0].takeman);
                        $("#txt_phone").val(data.itemMain[0].takephone);
                        $("#txt_address").val(data.itemMain[0].takeaddress)
                        $("#txt_remark").val(data.itemMain[0].remark)
                        docno = data.itemMain[0].docno;
                    }
                    addclickCount = addclickCount + data.itemMx.length;
                    if (data.itemMx.length > 0) {
                        for (var i = 0; i < data.itemMx.length; i++) {
                            arrtm.push(data.itemMx[i].tmid);
                            var str = '';
                            str += '<tr id="tr' + i + '">';
                            str += '<td style="display:none;">' + data.itemMx[i].tmid + '</td>';
                            str += '<td>' + data.itemMx[i].wmno + '</td >';
                            str += '<td>' + data.itemMx[i].sl + '</td>';
                            str += '<td><a href = "#" onclick = "removekh(' + i + ')" data - role="button" data - icon="delete" > 删除</a > </td > ';
                            str += '</tr>';
                            $("#productlist").append(str).trigger("create");
                        }
                    }
                }
            )
        })
        //验证条码
        function yztm() {
            var loginLoadIndex = layer.load(2);
            $.get("ashx/list.ashx?type=get_tm&tms=" + $("#txt_tm").val() + "&customerCodes=" + customerCode + "&r=" + Math.random(),
                { Action: "get" },
                function (data, textStatus) {
                    if (data.flag == true) {
                        layer.close(loginLoadIndex);
                        arrtm.push(data.tmid);
                        if (arrRepeat(arrtm)) {
                            layer.msg("此条码已经添加过，不能重复添加", function () {
                                time: 30000
                            });
                            arrtm.pop();
                        }
                        else {
                            var yz_zzs = /^[1-9]*[1-9][0-9]*$/;
                            layer.prompt({
                                formType: 0,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                                btn: ['确定', '取消'],
                                title: '填写数量',
                                area: ['50px', '100px'],
                                btnAlign: 'c',
                                content: '<div><input name="txt_sl" id="sl" class="layui-input"></div>',
                                yes: function (index, layero) {
                                    var value1 = $('#sl').val();//获取数量
                                    if (!yz_zzs.test(parseInt(value1))) {
                                        arrtm.removeByValue(data.tmid);
                                        layer.msg('必须是大于0的正整数！', { icon: 5 });
                                        return false;
                                    }
                                    else {
                                        layer.close(index);
                                        $("#txt_tm").val("")
                                        addclickCount++;
                                        var str = '';
                                        str += '<tr id="tr' + addclickCount + '">';
                                        str += '<td style="display:none;">' + data.tmid + '</td>';
                                        str += '<td>' + data.bjwm + '</td>';
                                        str += '<td>' + value1 + '</td>';
                                        str += '<td><a href = "#" onclick = "removekh(' + addclickCount + ')" data - role="button" data - icon="delete">删除</a> </td>';
                                        str += '</tr>';
                                        $("#productlist").append(str).trigger("create");
                                    }
                                },
                                btn2: function () {
                                    arrtm.removeByValue(data.tmid);
                                }, cancel: function () {
                                    arrtm.removeByValue(data.tmid);
                                }, end: function () {

                                }
                            });
                        } 
                    }
                    else {
                        layer.close(loginLoadIndex);
                        layer.msg(data.msg, function () {
                            time: 30000
                        });
                    }
                }
            )
        }
        //验证是否有重复条码
        function arrRepeat(arr) {
            var arrStr = JSON.stringify(arr), str;
            for (var i = 0; i < arr.length; i++) {
                if (arrStr.indexOf(arr[i]) != arrStr.lastIndexOf(arr[i])) {
                    return true;
                }
            };
            return false;
        }
        //删除指定行
        function removekh(delrow) {
            var trarr = $("#tr" + delrow + "").children();  //获取改行所有td
            var tdtm = trarr.eq(0).text()//获取改行的值
            arrtm.removeByValue(tdtm);
            $("#tr" + delrow + "").remove(); //根据行id移除该行
        }
        //删除数组指定元素
        Array.prototype.removeByValue = function (val) {
            for (var i = 0; i < this.length; i++) {
                if (this[i] == val) {
                    this.splice(i, 1);
                    break;
                }
            }
        }
        //正则表达式
        var reg = /^\d+(\.\d+)?$/; //只能为数字
        var msg = "只能填写数字!";
        //保存或者提交
        function saveandtj(lx) {
            var txt_shr = $("#txt_shr").val();//收货人
            var txt_phone = $("#txt_phone").val();//收货人手机号
            var txt_address = $("#txt_address").val();//收货地址
            var txt_remark = $("#txt_remark").val();//备注
            if (txt_shr == "" || txt_shr == undefined || txt_shr == null) {
                layer.msg("请填写收货人姓名", { time: 2000 })
                return false;
            }
            if (txt_phone == "" || txt_phone == undefined || txt_phone == null) {
                layer.msg("请填写手机号", { time: 2000 });
                return false;
            }
            if (txt_address == "" || txt_address == undefined || txt_address == null) {
                layer.msg("请填写收货地址", { time: 2000 });
                return false;
            }
            var ArrayObj_mx = new Array();
            var obj = document.getElementById("myTable");
            var rows = obj.rows;
            if (rows.length < 2) {
                layer.msg("请填写要调拨的条码明细！", { time: 2000 });
                return false;
            }
            else {
                for (var i = 1; i < rows.length; i++) { //由于第一行一般为标题，所以从第二行开始获取值）
                    var tmid = rows[i].cells[0].innerHTML;
                    var sl = rows[i].cells[2].innerHTML;
                    var mx = {
                        "tmid": tmid,
                        "sl": sl
                    };
                    ArrayObj_mx.push(mx);
                }
                //添加基本信息1
                var ArrayObj_main = new Array();
                var main = {
                    "id": ids,
                    "docno": docno,
                    "shr": txt_shr,
                    "phone": txt_phone,
                    "address": txt_address,
                    "remark": txt_remark,
                    "customerCode": customerCode
                };
                ArrayObj_main.push(main);
                if (ArrayObj_main.length > 0 && ArrayObj_mx.length > 0) {
                    //防止多次点击
                    if (clickCount >= 2) {
                        return false;
                    }
                    else {
                        var loginLoadIndex = layer.load(2);
                        $.ajax({
                            type: "post",
                            async: true,            //异步请求（同步请求将会锁住浏览器，用户其他操作必须等待请求完成才可以执行）
                            url: "ashx/list.ashx?type=editbydocno&lxs=" + lx,
                            data: JSON.stringify({ 'ArrayObj_main': ArrayObj_main, 'ArrayObj_mx': ArrayObj_mx }),
                            dataType: "json",        //返回数据形式为json
                            success: function (data) {
                                if (data.flag == "True") {
                                    layer.close(loginLoadIndex);
                                    clickCount++;
                                    layer.msg(data.remsg, {
                                        time: 2000,
                                        end: function () {
                                            location.href = "sq_list.html?customerCode=" + customerCode + "&mobile=" + mobile;;
                                        }
                                    })
                                }
                                else {
                                    layer.close(loginLoadIndex);
                                    layer.msg(data.remsg, function () {
                                        time: 30000
                                    });
                                }
                            }
                        });
                    }
                }
                else {
                    layer.msg("失败，请联系系统管理员！", function () {
                        time: 30000
                    });
                }
            }
        }
        //删除
        function zfbyid() {
            layer.confirm('确定作废吗？', function (index) {
                //执行 Ajax 后重载
                $.ajax({
                    url: 'ashx/list.ashx?type=delete&getids=' + ids,
                    type: "post",
                    contentType: "application/json",
                    dataType: "json",
                    data: {},
                    success: function (res) {
                        if (res.flag == "True") {
                            layer.msg(res.remsg, {
                                time: 2000,
                                end: function () {
                                    location.href = "sq_list.html?customerCode=" + customerCode + "&mobile=" + mobile;;
                                }
                            })
                        }
                        else {
                            layer.alert(res.remsg);
                        }
                    }
                });
            });
        }
        //重新发布
        function againbyid() {
            layer.confirm('确定取消抢单并重新发布吗？', function (index) {
                //执行 Ajax 后重载
                $.ajax({
                    url: 'ashx/list.ashx?type=againfabu&getids=' + ids,
                    type: "post",
                    contentType: "application/json",
                    dataType: "json",
                    data: {},
                    success: function (res) {
                        if (res.flag == "True") {
                            layer.msg(res.remsg, {
                                time: 2000,
                                end: function () {
                                    location.href = "sq_list.html?customerCode=" + customerCode + "&mobile=" + mobile;;
                                }
                            })
                        }
                        else {
                            layer.alert(res.remsg);
                        }
                    }
                });
            });
        }
        //取消未抢单的数量
        function deletewqd() {
            layer.confirm('确定取消未被抢单的商品吗？', function (index) {
                //执行 Ajax 后重载
                $.ajax({
                    url: 'ashx/list.ashx?type=deletewqd&getids=' + ids,
                    type: "post",
                    contentType: "application/json",
                    dataType: "json",
                    data: {},
                    success: function (res) {
                        if (res.flag == "True") {
                            layer.msg(res.remsg, {
                                time: 2000,
                                end: function () {
                                    location.href = "sq_list.html?customerCode=" + customerCode + "&mobile=" + mobile;;
                                }
                            })
                        }
                        else {
                            layer.alert(res.remsg);
                        }
                    }
                });
            });
        }
        //自动转为大写
        function toUpperCase(obj) {
            obj.value = obj.value.toUpperCase()
        }
    </script>
    <style type="text/css">
        div#findBar.ui-grid-a .ui-block-a {
            width: 80%
        }
        div#findBar.ui-grid-a .ui-block-b {
            width: 20%
        }
        div#findIput.ui-grid-a .ui-block-a {
            width: 25%
        }
        div#findIput.ui-grid-a .ui-block-b {
            width: 75%
        }
        body {
            margin: 0;
            padding: 0;
        }
        .table-responsive {
            max-width: 375px;
            overflow: auto;
            margin: 50px auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            border-spacing: 0;
            border: 0px solid #999;
            table-layout: fixed;
        }
        th, td {
            border: 0px solid #999;
        }
        td {
            padding: 0 4px;
            white-space: pre-line;
            word-break: break-all;
            word-wrap: break-word;
        }
        .ui-table-columntoggle-btn .ui-btn .ui-btn-a .ui-corner-all .ui-shadow .ui-mini {
            display: none;
        }
        textarea.ui-input-text {
            min-height: 80px;
        }
        div#div_product1 a[href="#myTable-popup"] {
            display: none;
        }
    </style>
</head>
<body>
    <div data-role="page" id="pageone">
        <div data-role="header" data-position="fixed">
            <h1>新增</h1>
            <a href="#" class="ui-btn-left" data-rel="back">返回</a>
        </div>
        <div data-role="content" class="ui-content">
            <form method="post" action="">
                <div class="ui-field-contain" data-role="fieldcontain">
                    <div id="findIput" class="ui-grid-a">
                        <div class="ui-block-a">
                            <label for="lname">店铺名称</label>
                        </div>
                        <div class="ui-block-b">
                            <input type="text" name="lname" id="txt_storename" disabled="disabled" data-clear-btn="true">
                        </div>
                    </div>
                    <div id="findIput" class="ui-grid-a">
                        <div class="ui-block-a">
                            <label for="lname">收货人</label>
                        </div>
                        <div class="ui-block-b">
                            <input type="text" name="lname" id="txt_shr" placeholder="填写收货人..." data-clear-btn="true">
                        </div>
                    </div>
                    <div id="findIput" class="ui-grid-a">
                        <div class="ui-block-a">
                            <label for="lname">手机号码</label>
                        </div>
                        <div class="ui-block-b">
                            <input type="text" name="lname" id="txt_phone" placeholder="填写手机号码..." data-clear-btn="true">
                        </div>
                    </div>
                    <div id="findIput" class="ui-grid-a">
                        <div class="ui-block-a">
                            <label for="lname">收货地址</label>
                        </div>
                        <div class="ui-block-b">
                            <textarea name="txt_address" id="txt_address" placeholder="填写收货地址.." data-clear-btn="true"></textarea>
                        </div>
                    </div>
                    <div id="findIput" class="ui-grid-a">
                        <div class="ui-block-a">
                            <label for="lname">备注</label>
                        </div>
                        <div class="ui-block-b">
                            <textarea name="txt_remark" id="txt_remark" placeholder="填写备注.." data-clear-btn="true"></textarea>
                        </div>
                    </div>
                    <!--<label for="lname">拆分发货</label>
                <input type="checkbox" data-role="flipswitch" name="switch" id="switch" checked>-->
                    <!--<label for="lname">快递类型</label>
                <select name="kdlx" id="kdlx">
                    <option value="sfbj">顺丰标件</option>
                </select>-->
                </div>
                <p style="color:red;">温馨提示：请把相同收货地址的货品集中在同一单下单，否则会产出额外的快递费用！！！</p>
                <div id="findBar" class="ui-grid-a">
                    <div class="ui-block-a">
                        <input type="text" onkeyup="toUpperCase(this)" data-inline="true" name="lname" id="txt_tm" placeholder="填写条形码..." data-clear-btn="true">
                    </div>
                    <div class="ui-block-b">
                        <a href="#" onclick="yztm()" class="ui-btn ui-btn-inline" data-mini="true">确定</a>
                    </div>
                </div>
                <div data-role="fieldcontain" class="data-table" id="div_product1">
                    <table data-role="table" data-mode="columntoggle" class="ui-responsive" data-column-btn-text="点我显示或隐藏列!" id="myTable">
                        <thead>
                            <tr>
                                <th style="display:none;">条码id</th>
                                <th style="width:65%">条码</th>
                                <th style="width:15%">数量</th>
                                <th style="width:20%">操作</th>
                            </tr>
                        </thead>
                        <tbody id="productlist"></tbody>
                    </table>
                </div>
            </form>
        </div>
        <div data-role="footer" style="text-align:center;" data-position="fixed">
            <div id="saveandtj">
                <a href="#" onclick="saveandtj('save')" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">保存</a>
                <a href="#" onclick="saveandtj('tj')" class="ui-btn ui-corner-all ui-shadow ui-icon-plus ui-btn-icon-left">发布</a>
                <a href="#" onclick="zfbyid()" class="ui-btn ui-corner-all ui-shadow ui-icon-delete ui-btn-icon-left">作废</a>
            </div>
            <div id="config">
                <a href="#" onclick="zfbyid()" class="ui-btn ui-corner-all ui-shadow ui-icon-delete ui-btn-icon-left">作废</a>
                <a href="#" onclick="againbyid()" class="ui-btn ui-corner-all ui-shadow ui-icon-delete ui-btn-icon-left">重新发布</a>
                <a href="#" onclick="deletewqd()" class="ui-btn ui-corner-all ui-shadow ui-icon-delete ui-btn-icon-left">取消未抢单</a>
            </div>
        </div>
    </div>
</body>
</html>
